/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "ecmascript/tests/test_helper.h"

#include "common_components/heap/w_collector/w_collector.h"
#include "common_components/heap/collector/collector_proxy.h"
#include "common_components/heap/heap_manager.h"
#include "common_components/heap/allocator/region_desc.h"
#include "common_components/base_runtime/base_runtime_param.h"
#include "ecmascript/mem/dynamic_object_operator.h"
#include "ecmascript/js_hclass.h"
#include "ecmascript/mem/tagged_object.h"

using namespace common;

namespace panda::test {
class WCollectorTest : public BaseTestWithScope<false> {};

std::unique_ptr<WCollector> GetWCollector()
{
    CollectorResources& resources = Heap::GetHeap().GetCollectorResources();
    Allocator& allocator = Heap::GetHeap().GetAllocator();
    
    return std::make_unique<WCollector>(allocator, resources);
}

HWTEST_F_L0(WCollectorTest, IsUnmovableFromObjectTest0) {
    std::unique_ptr<WCollector> wcollector = GetWCollector();
    ASSERT_TRUE(wcollector != nullptr);

    BaseObject* obj = nullptr;
    EXPECT_FALSE(wcollector->IsUnmovableFromObject(obj));
}

HWTEST_F_L0(WCollectorTest, IsUnmovableFromObjectTest1) {
    std::unique_ptr<WCollector> wcollector = GetWCollector();
    ASSERT_TRUE(wcollector != nullptr);

    HeapAddress addr = HeapManager::Allocate(sizeof(BaseObject), AllocType::MOVEABLE_OBJECT, true);
    BaseObject *obj = reinterpret_cast<BaseObject*>(addr);
    
    new (obj) BaseObject();

    EXPECT_FALSE(wcollector->IsUnmovableFromObject(obj));
    EXPECT_FALSE(wcollector->ResurrectObject(obj));
}

HWTEST_F_L0(WCollectorTest, IsUnmovableFromObjectTest2) {
    std::unique_ptr<WCollector> wcollector = GetWCollector();
    ASSERT_TRUE(wcollector != nullptr);

    HeapAddress addr = HeapManager::Allocate(sizeof(BaseObject), AllocType::PINNED_OBJECT, true);
    BaseObject *obj = reinterpret_cast<BaseObject*>(addr);
    
    new (obj) BaseObject();

    RegionDesc* region = RegionDesc::GetRegionDescAt(addr);

    bool isMarked = region->GetOrAllocResurrectBitmap()->MarkBits(0);
    region->SetResurrectedRegionFlag(1);
    region->SetRegionType(RegionDesc::RegionType::EXEMPTED_FROM_REGION);

    EXPECT_FALSE(isMarked);

    EXPECT_TRUE(wcollector->IsUnmovableFromObject(obj));
    EXPECT_TRUE(wcollector->ResurrectObject(obj));
}

HWTEST_F_L0(WCollectorTest, MarkNewObjectTest0) {
    std::unique_ptr<WCollector> wcollector = GetWCollector();
    ASSERT_TRUE(wcollector != nullptr);

    HeapAddress addr = HeapManager::Allocate(sizeof(BaseObject), AllocType::MOVEABLE_OBJECT, true);
    BaseObject *obj = reinterpret_cast<BaseObject*>(addr);
    
    new (obj) BaseObject();

    Mutator::GetMutator()->SetMutatorPhase(GCPhase::GC_PHASE_MARK);

    obj->SetForwardState(BaseStateWord::ForwardState::FORWARDED);
    EXPECT_TRUE(obj->IsForwarded());

    wcollector->MarkNewObject(obj);
    EXPECT_TRUE(RegionSpace::IsMarkedObject(obj));
}

HWTEST_F_L0(WCollectorTest, MarkNewObjectTest1) {
    std::unique_ptr<WCollector> wcollector = GetWCollector();
    ASSERT_TRUE(wcollector != nullptr);

    HeapAddress addr = HeapManager::Allocate(sizeof(BaseObject), AllocType::MOVEABLE_OBJECT, true);
    BaseObject *obj = reinterpret_cast<BaseObject*>(addr);
    
    new (obj) BaseObject();

    Mutator::GetMutator()->SetMutatorPhase(GCPhase::GC_PHASE_ENUM);

    obj->SetForwardState(BaseStateWord::ForwardState::FORWARDED);
    EXPECT_TRUE(obj->IsForwarded());

    wcollector->MarkNewObject(obj);
    EXPECT_TRUE(RegionSpace::IsMarkedObject(obj));
}

HWTEST_F_L0(WCollectorTest, MarkNewObjectTest2) {
    std::unique_ptr<WCollector> wcollector = GetWCollector();
    ASSERT_TRUE(wcollector != nullptr);

    HeapAddress addr = HeapManager::Allocate(sizeof(BaseObject), AllocType::MOVEABLE_OBJECT, true);
    BaseObject *obj = reinterpret_cast<BaseObject*>(addr);
    
    new (obj) BaseObject();

    Mutator::GetMutator()->SetMutatorPhase(GCPhase::GC_PHASE_REMARK_SATB);

    obj->SetForwardState(BaseStateWord::ForwardState::FORWARDED);
    EXPECT_TRUE(obj->IsForwarded());

    wcollector->MarkNewObject(obj);
    EXPECT_TRUE(RegionSpace::IsMarkedObject(obj));
}

HWTEST_F_L0(WCollectorTest, MarkNewObjectTest3) {
    std::unique_ptr<WCollector> wcollector = GetWCollector();
    ASSERT_TRUE(wcollector != nullptr);

    HeapAddress addr = HeapManager::Allocate(sizeof(BaseObject), AllocType::MOVEABLE_OBJECT, true);
    BaseObject *obj = reinterpret_cast<BaseObject*>(addr);
    
    new (obj) BaseObject();

    Mutator::GetMutator()->SetMutatorPhase(GCPhase::GC_PHASE_FINAL_MARK);

    obj->SetForwardState(BaseStateWord::ForwardState::FORWARDED);
    EXPECT_TRUE(obj->IsForwarded());

    wcollector->MarkNewObject(obj);
    EXPECT_FALSE(RegionSpace::IsMarkedObject(obj));
}

HWTEST_F_L0(WCollectorTest, ForwardObjectTest0) {
    ThreadLocal::SetThreadType(ThreadType::GC_THREAD);

    std::unique_ptr<WCollector> wcollector = GetWCollector();
    ASSERT_TRUE(wcollector != nullptr);

    HeapAddress addr = HeapManager::Allocate((sizeof(ecmascript::JSHClass) + sizeof(BaseClass)), AllocType::MOVEABLE_OBJECT, true);
    BaseObject *obj = reinterpret_cast<BaseObject*>(addr);
    
    new (obj) BaseObject();

    auto regionInfo = RegionDesc::GetRegionDescAt(reinterpret_cast<uintptr_t>(obj));
    regionInfo->SetRegionType(RegionDesc::RegionType::TO_REGION);

    wcollector->SetGCPhase(GCPhase::GC_PHASE_PRECOPY);
    obj->SetForwardState(BaseStateWord::ForwardState::NORMAL);
    obj->SetFullBaseClassWithoutBarrier(reinterpret_cast<BaseClass *>(addr + sizeof(BaseObject)));

    auto taggedObject = ecmascript::TaggedObject::Cast(obj);
    ecmascript::JSHClass *jsHclass = taggedObject->GetClass();
    EXPECT_EQ(reinterpret_cast<HeapAddress>(jsHclass), addr + sizeof(BaseObject));

    jsHclass->SetObjectType(ecmascript::JSType::TAGGED_ARRAY);

    ecmascript::DynamicObjectOperator dynamicObjOp;
    obj->RegisterDynamic(reinterpret_cast<BaseObjectOperatorInterfaces *>(&dynamicObjOp));

    BaseObject *newObj = wcollector->ForwardObject(obj);
    EXPECT_NE(static_cast<void *>(obj), static_cast<void *>(newObj));
    EXPECT_EQ(newObj->GetBaseStateWord().GetForwardState(), BaseStateWord::ForwardState::NORMAL);

    ThreadLocal::SetThreadType(ThreadType::ARK_PROCESSOR);
    regionInfo->SetRegionType(RegionDesc::RegionType::THREAD_LOCAL_REGION);
}

HWTEST_F_L0(WCollectorTest, ForwardObjectTest1) {
    ThreadLocal::SetThreadType(ThreadType::GC_THREAD);

    std::unique_ptr<WCollector> wcollector = GetWCollector();
    ASSERT_TRUE(wcollector != nullptr);

    HeapAddress addr = HeapManager::Allocate(sizeof(BaseObject), AllocType::MOVEABLE_OBJECT, true);
    BaseObject *obj = reinterpret_cast<BaseObject*>(addr);
    
    new (obj) BaseObject();
    wcollector->SetGCPhase(GCPhase::GC_PHASE_PRECOPY);
    obj->SetForwardState(BaseStateWord::ForwardState::FORWARDED);

    ecmascript::DynamicObjectOperator dynamicObjOp;
    obj->RegisterDynamic(reinterpret_cast<BaseObjectOperatorInterfaces *>(&dynamicObjOp));

    BaseObject *newObj = wcollector->ForwardObject(obj);

    EXPECT_EQ(newObj->GetSize(), 0);
    EXPECT_EQ(obj->GetSize(), 0);

    ThreadLocal::SetThreadType(ThreadType::ARK_PROCESSOR);
}

HWTEST_F_L0(WCollectorTest, ForwardObjectTest2) {
    ThreadLocal::SetThreadType(ThreadType::GC_THREAD);

    std::unique_ptr<WCollector> wcollector = GetWCollector();
    ASSERT_TRUE(wcollector != nullptr);

    HeapAddress addr = HeapManager::Allocate(sizeof(BaseObject), AllocType::MOVEABLE_OBJECT, true);
    BaseObject *obj = reinterpret_cast<BaseObject*>(addr);
    
    new (obj) BaseObject();

    wcollector->SetGCPhase(GCPhase::GC_PHASE_COPY);

    obj->SetForwardState(BaseStateWord::ForwardState::FORWARDED);

    ecmascript::DynamicObjectOperator dynamicObjOp;
    obj->RegisterDynamic(reinterpret_cast<BaseObjectOperatorInterfaces *>(&dynamicObjOp));

    BaseObject *newObj = wcollector->ForwardObject(obj);
    EXPECT_EQ(static_cast<void *>(obj), static_cast<void *>(newObj));

    ThreadLocal::SetThreadType(ThreadType::ARK_PROCESSOR);
}

HWTEST_F_L0(WCollectorTest, ForwardObjectTest3) {
    ThreadLocal::SetThreadType(ThreadType::GC_THREAD);

    std::unique_ptr<WCollector> wcollector = GetWCollector();
    ASSERT_TRUE(wcollector != nullptr);

    HeapAddress addr = HeapManager::Allocate(sizeof(BaseObject), AllocType::MOVEABLE_OBJECT, true);
    BaseObject *obj = reinterpret_cast<BaseObject*>(addr);
    
    new (obj) BaseObject();

    wcollector->SetGCPhase(GCPhase::GC_PHASE_FIX);

    obj->SetForwardState(BaseStateWord::ForwardState::FORWARDED);

    ecmascript::DynamicObjectOperator dynamicObjOp;
    obj->RegisterDynamic(reinterpret_cast<BaseObjectOperatorInterfaces *>(&dynamicObjOp));

    BaseObject *newObj = wcollector->ForwardObject(obj);
    EXPECT_EQ(static_cast<void *>(obj), static_cast<void *>(newObj));

    ThreadLocal::SetThreadType(ThreadType::ARK_PROCESSOR);
}

HWTEST_F_L0(WCollectorTest, ForwardObjectTest4) {
    std::unique_ptr<WCollector> wcollector = GetWCollector();
    ASSERT_TRUE(wcollector != nullptr);

    HeapAddress addr = HeapManager::Allocate(sizeof(BaseObject), AllocType::MOVEABLE_OBJECT, true);
    BaseObject *obj = reinterpret_cast<BaseObject*>(addr);
    
    new (obj) BaseObject();

    Mutator::GetMutator()->SetMutatorPhase(GCPhase::GC_PHASE_PRECOPY);
    obj->SetForwardState(BaseStateWord::ForwardState::FORWARDED);

    ecmascript::DynamicObjectOperator dynamicObjOp;
    obj->RegisterDynamic(reinterpret_cast<BaseObjectOperatorInterfaces *>(&dynamicObjOp));

    BaseObject *newObj = wcollector->ForwardObject(obj);
    EXPECT_EQ(static_cast<void *>(obj), static_cast<void *>(newObj));
}

HWTEST_F_L0(WCollectorTest, ForwardObjectTest5) { 
    std::unique_ptr<WCollector> wcollector = GetWCollector();
    ASSERT_TRUE(wcollector != nullptr);

    HeapAddress addr = HeapManager::Allocate(sizeof(BaseObject), AllocType::MOVEABLE_OBJECT, true);
    BaseObject *obj = reinterpret_cast<BaseObject*>(addr);
    
    new (obj) BaseObject();

    Mutator::GetMutator()->SetMutatorPhase(GCPhase::GC_PHASE_COPY);
    obj->SetForwardState(BaseStateWord::ForwardState::FORWARDED);

    ecmascript::DynamicObjectOperator dynamicObjOp;
    obj->RegisterDynamic(reinterpret_cast<BaseObjectOperatorInterfaces *>(&dynamicObjOp));

    BaseObject *newObj = wcollector->ForwardObject(obj);
    EXPECT_EQ(static_cast<void *>(obj), static_cast<void *>(newObj));
}

HWTEST_F_L0(WCollectorTest, ForwardObjectTest6) {
    std::unique_ptr<WCollector> wcollector = GetWCollector();
    ASSERT_TRUE(wcollector != nullptr);

    HeapAddress addr = HeapManager::Allocate((sizeof(ecmascript::JSHClass) + sizeof(BaseClass)), AllocType::MOVEABLE_OBJECT, true);
    BaseObject *obj = reinterpret_cast<BaseObject*>(addr);
    
    new (obj) BaseObject();

    auto regionInfo = RegionDesc::GetRegionDescAt(reinterpret_cast<uintptr_t>(obj));
    regionInfo->SetRegionType(RegionDesc::RegionType::TO_REGION);

    Mutator::GetMutator()->SetMutatorPhase(GCPhase::GC_PHASE_FIX);
    obj->SetForwardState(BaseStateWord::ForwardState::NORMAL);
    obj->SetFullBaseClassWithoutBarrier(reinterpret_cast<BaseClass *>(addr + sizeof(BaseObject)));

    ecmascript::DynamicObjectOperator dynamicObjOp;
    obj->RegisterDynamic(reinterpret_cast<BaseObjectOperatorInterfaces *>(&dynamicObjOp));

    auto taggedObject = ecmascript::TaggedObject::Cast(obj);
    ecmascript::JSHClass *jsHclass = taggedObject->GetClass();
    EXPECT_EQ(reinterpret_cast<HeapAddress>(jsHclass), addr + sizeof(BaseObject));

    jsHclass->SetObjectType(ecmascript::JSType::TAGGED_ARRAY);

    BaseObject *newObj = wcollector->ForwardObject(obj);
    EXPECT_NE(static_cast<void *>(obj), static_cast<void *>(newObj));
    EXPECT_EQ(newObj->GetBaseStateWord().GetForwardState(), BaseStateWord::ForwardState::NORMAL);

    regionInfo->SetRegionType(RegionDesc::RegionType::THREAD_LOCAL_REGION);
}

HWTEST_F_L0(WCollectorTest, ForwardUpdateRawRefTest0) {
    std::unique_ptr<WCollector> wcollector = GetWCollector();
    ASSERT_TRUE(wcollector != nullptr);

    HeapAddress addr = HeapManager::Allocate(sizeof(BaseObject), AllocType::MOVEABLE_OBJECT, true);
    BaseObject *obj = reinterpret_cast<BaseObject*>(addr);
    
    new (obj) BaseObject();

    common::ObjectRef root = {obj};

    BaseObject *oldObj = wcollector->ForwardUpdateRawRef(root);
    EXPECT_EQ(oldObj, obj);
}

class WCollectorMockTest : public BaseTestWithScope<false> {
public:
    void SetUp() override
    {
        BaseRuntime::GetInstance()->Init();
        MutatorManager::Instance().CreateRuntimeMutator(ThreadType::GC_THREAD);
    }

    void TearDown() override
    {
        MutatorManager::Instance().DestroyRuntimeMutator(ThreadType::GC_THREAD);
        ThreadLocal::SetThreadType(ThreadType::ARK_PROCESSOR);

        BaseRuntime::GetInstance()->Fini();
        BaseRuntime::GetInstance()->DestroyInstance();
        ecmascript::TaggedStateWord::BASE_ADDRESS = 0;
    }
};

class WCollectorMock : public WCollector {
public:
    explicit WCollectorMock(Allocator& allocator, CollectorResources& resources)
        : WCollector(allocator, resources) {}

    ~WCollectorMock() override = default;

    bool TryUpdateRefFieldMock(BaseObject* obj, RefField<>& field, BaseObject*& newRef)
    {
        return this->TryUpdateRefField(obj, field, newRef);
    }

    bool TryForwardRefFieldMock(BaseObject* obj, RefField<>& field, BaseObject*& newRef)
    {
        return this->TryForwardRefField(obj, field, newRef);
    }

    bool TryUntagRefFieldMock(BaseObject* obj, RefField<>& field, BaseObject*& target)
    {
        return this->TryUntagRefField(obj, field, target);
    }

    void EnumAndTagRawRootMock(common::ObjectRef& ref, RootSet& rootSet)
    {
        this->EnumAndTagRawRoot(ref, rootSet);
    }

    void DoGarbageCollectionMock()
    {
        this->DoGarbageCollection();
    }

    void ProcessWeakReferencesMock()
    {
        this->ProcessWeakReferences();
    }
};

std::unique_ptr<WCollectorMock> GetWCollectorMock()
{
    CollectorResources& resources = Heap::GetHeap().GetCollectorResources();
    Allocator& allocator = Heap::GetHeap().GetAllocator();
    
    return std::make_unique<WCollectorMock>(allocator, resources);
}

HWTEST_F_L0(WCollectorTest, DoGarbageCollectionTest0) {
    std::unique_ptr<WCollector> wcollector = GetWCollector();
    ASSERT_TRUE(wcollector != nullptr);

    std::unique_ptr<WCollectorMock> wcollectorMock = GetWCollectorMock();
    ASSERT_TRUE(wcollectorMock != nullptr);

    RuntimeParam runtimePara = BaseRuntimeParam::DefaultRuntimeParam();
    runtimePara.gcParam.enableStwGC = true;
    wcollectorMock->Init(runtimePara);

    Mutator::GetMutator()->SetMutatorPhase(GCPhase::GC_PHASE_MARK);
    Mutator::GetMutator()->SetSuspensionFlag(Mutator::SuspensionType::SUSPENSION_FOR_GC_PHASE);
    Mutator::GetMutator()->TransitionGCPhase(true);

    wcollectorMock->DoGarbageCollectionMock();
    EXPECT_EQ(wcollectorMock->GetGCPhase(), GCPhase::GC_PHASE_IDLE);
}

HWTEST_F_L0(WCollectorTest, TryUpdateRefFieldTest0) {
    std::unique_ptr<WCollector> wcollector = GetWCollector();
    ASSERT_TRUE(wcollector != nullptr);

    std::unique_ptr<WCollectorMock> wcollectorMock = GetWCollectorMock();
    ASSERT_TRUE(wcollectorMock != nullptr);

    HeapAddress addr = HeapManager::Allocate(sizeof(BaseObject), AllocType::MOVEABLE_OBJECT, true);
    BaseObject *obj = reinterpret_cast<BaseObject*>(addr);
    new (obj) BaseObject();

    RefField<false> field(obj);
    field.SetTargetObject(obj);
    EXPECT_EQ(field.GetTargetObject(), obj);
    BaseObject* newObj;

    EXPECT_FALSE(wcollectorMock->TryUpdateRefFieldMock(obj, field, newObj));
}

HWTEST_F_L0(WCollectorTest, TryUpdateRefFieldTest1) {
    std::unique_ptr<WCollectorMock> wcollectorMock = GetWCollectorMock();
    ASSERT_TRUE(wcollectorMock != nullptr);

    HeapAddress addr = HeapManager::Allocate(sizeof(BaseObject), AllocType::MOVEABLE_OBJECT, true);
    BaseObject *obj = reinterpret_cast<BaseObject*>(addr);
    new (obj) BaseObject();

    RegionDesc* region = RegionDesc::GetRegionDescAt(addr);
    region->SetRegionType(RegionDesc::RegionType::FROM_REGION);

    RefField<false> field(obj);
    field.SetTargetObject(obj);
    EXPECT_EQ(field.GetTargetObject(), obj);

    BaseObject* newObj;

    EXPECT_FALSE(wcollectorMock->TryUpdateRefFieldMock(obj, field, newObj));
    EXPECT_EQ(newObj, nullptr);

    region->SetRegionType(RegionDesc::RegionType::THREAD_LOCAL_REGION);
}

HWTEST_F_L0(WCollectorTest, TryUpdateRefFieldTest2) {
    std::unique_ptr<WCollectorMock> wcollectorMock = GetWCollectorMock();
    ASSERT_TRUE(wcollectorMock != nullptr);

    size_t allocSize = sizeof(BaseObject) + sizeof(BaseObject) + sizeof(BaseClass);
    HeapAddress addr = HeapManager::Allocate(allocSize, AllocType::MOVEABLE_OBJECT, true);

    RegionDesc* region = RegionDesc::GetRegionDescAt(addr);
    region->SetRegionType(RegionDesc::RegionType::FROM_REGION);

    BaseObject *obj = reinterpret_cast<BaseObject*>(addr);
    new (obj) BaseObject();

    size_t addrOffset = sizeof(BaseObject);
    BaseObject *objFwd = reinterpret_cast<BaseObject*>(addr + addrOffset);
    new (objFwd) BaseObject();

    addrOffset += sizeof(BaseObject);
    objFwd->SetFullBaseClassWithoutBarrier(reinterpret_cast<BaseClass *>(addr + addrOffset));

    ecmascript::DynamicObjectOperator dynamicObjOp;
    BaseObject::RegisterDynamic(reinterpret_cast<BaseObjectOperatorInterfaces *>(&dynamicObjOp));
    
    obj->SetForwardState(BaseStateWord::ForwardState::FORWARDING);
    obj->SetForwardingPointerAfterExclusive(objFwd);

    RefField<false> field(obj);
    field.SetTargetObject(obj);
    EXPECT_EQ(field.GetTargetObject(), obj);

    BaseObject* newObj;

    EXPECT_TRUE(wcollectorMock->TryUpdateRefFieldMock(obj, field, newObj));
    EXPECT_EQ(newObj, objFwd);

    region->SetRegionType(RegionDesc::RegionType::THREAD_LOCAL_REGION);
}

HWTEST_F_L0(WCollectorTest, TryUpdateRefFieldTest3) {
    std::unique_ptr<WCollectorMock> wcollectorMock = GetWCollectorMock();
    ASSERT_TRUE(wcollectorMock != nullptr);

    size_t allocSize = sizeof(BaseObject) + sizeof(BaseObject);
    HeapAddress addr = HeapManager::Allocate(allocSize, AllocType::MOVEABLE_OBJECT, true);
    
    RegionDesc* region = RegionDesc::GetRegionDescAt(addr);
    region->SetRegionType(RegionDesc::RegionType::FROM_REGION);

    BaseObject *obj = reinterpret_cast<BaseObject*>(addr);
    new (obj) BaseObject();

    size_t addrOffset = sizeof(BaseObject);
    BaseObject *objFwd = reinterpret_cast<BaseObject*>(addr + addrOffset);
    new (objFwd) BaseObject();

    addrOffset += sizeof(BaseObject);
    objFwd->SetFullBaseClassWithoutBarrier(reinterpret_cast<BaseClass *>(addr + addrOffset));

    ecmascript::DynamicObjectOperator dynamicObjOp;
    BaseObject::RegisterDynamic(reinterpret_cast<BaseObjectOperatorInterfaces *>(&dynamicObjOp));
    
    obj->SetForwardState(BaseStateWord::ForwardState::FORWARDING);
    obj->SetForwardingPointerAfterExclusive(objFwd);

    RefField<false> field(obj);
    field.SetTargetObject(obj);
    EXPECT_EQ(field.GetTargetObject(), obj);

    BaseObject* newObj;

    EXPECT_TRUE(wcollectorMock->TryUpdateRefFieldMock(nullptr, field, newObj));
    EXPECT_EQ(newObj, objFwd);

    region->SetRegionType(RegionDesc::RegionType::THREAD_LOCAL_REGION);
}

HWTEST_F_L0(WCollectorTest, TryForwardRefFieldTest0) {
    std::unique_ptr<WCollectorMock> wcollectorMock = GetWCollectorMock();
    ASSERT_TRUE(wcollectorMock != nullptr);

    Mutator::GetMutator()->SetMutatorPhase(GCPhase::GC_PHASE_PRECOPY);

    HeapAddress addr = HeapManager::Allocate(sizeof(BaseObject), AllocType::MOVEABLE_OBJECT, true);
    BaseObject *obj = reinterpret_cast<BaseObject*>(addr);
    new (obj) BaseObject();

    obj->SetForwardState(BaseStateWord::ForwardState::FORWARDED);

    ecmascript::DynamicObjectOperator dynamicObjOp;
    BaseObject::RegisterDynamic(reinterpret_cast<BaseObjectOperatorInterfaces *>(&dynamicObjOp));

    RegionDesc* region = RegionDesc::GetRegionDescAt(addr);
    region->SetRegionType(RegionDesc::RegionType::FROM_REGION);

    RefField<false> field(obj);
    field.SetTargetObject(obj);
    EXPECT_EQ(field.GetTargetObject(), obj);

    BaseObject* newObj;

    EXPECT_FALSE(wcollectorMock->TryForwardRefFieldMock(obj, field, newObj));
    EXPECT_EQ(newObj, nullptr);

    region->SetRegionType(RegionDesc::RegionType::THREAD_LOCAL_REGION);
}

HWTEST_F_L0(WCollectorTest, TryUntagRefFieldTest0) {
    std::unique_ptr<WCollectorMock> wcollectorMock = GetWCollectorMock();
    ASSERT_TRUE(wcollectorMock != nullptr);

    HeapAddress addr = HeapManager::Allocate(sizeof(BaseObject), AllocType::MOVEABLE_OBJECT, true);
    BaseObject *obj = reinterpret_cast<BaseObject*>(addr);
    new (obj) BaseObject();

    RefField<false> field(obj);
    field.SetTargetObject(obj);
    EXPECT_EQ(field.GetTargetObject(), obj);
    BaseObject* newObj;

    EXPECT_FALSE(wcollectorMock->TryUntagRefFieldMock(obj, field, newObj));
}

HWTEST_F_L0(WCollectorTest, EnumAndTagRawRootTest0) {
    std::unique_ptr<WCollectorMock> wcollectorMock = GetWCollectorMock();
    ASSERT_TRUE(wcollectorMock != nullptr);

    BaseObject *obj = nullptr;
    common::ObjectRef root = {obj};
    TraceCollector::RootSet rootSet;

    wcollectorMock->EnumAndTagRawRootMock(root, rootSet);
}

HWTEST_F_L0(WCollectorTest, EnumAndTagRawRootTest1) {
    std::unique_ptr<WCollectorMock> wcollectorMock = GetWCollectorMock();
    ASSERT_TRUE(wcollectorMock != nullptr);

    size_t allocSize = sizeof(BaseObject) + sizeof(BaseClass) + sizeof(BaseClass);
    HeapAddress addr = HeapManager::Allocate(allocSize, AllocType::MOVEABLE_OBJECT, true);
    BaseObject *obj = reinterpret_cast<BaseObject*>(addr);
    new (obj) BaseObject();

    size_t addrOffset = sizeof(BaseObject);
    obj->SetFullBaseClassWithoutBarrier(reinterpret_cast<BaseClass *>(addr + addrOffset));

    ecmascript::DynamicObjectOperator dynamicObjOp;
    BaseObject::RegisterDynamic(reinterpret_cast<BaseObjectOperatorInterfaces *>(&dynamicObjOp));

    auto taggedObject = ecmascript::TaggedObject::Cast(obj);
    ecmascript::JSHClass *jsHclass = taggedObject->GetClass();
    EXPECT_EQ(reinterpret_cast<HeapAddress>(jsHclass), addr + addrOffset);
    jsHclass->SetObjectType(ecmascript::JSType::HCLASS);

    addrOffset += sizeof(BaseClass);
    jsHclass->SetFullBaseClassWithoutBarrier(reinterpret_cast<BaseClass *>(addr + addrOffset));
    EXPECT_EQ(reinterpret_cast<HeapAddress>(jsHclass->GetClass()), addr + addrOffset);
    jsHclass->GetClass()->SetObjectType(ecmascript::JSType::HCLASS);

    common::ObjectRef root = {obj};
    TraceCollector::RootSet rootSet;

    wcollectorMock->EnumAndTagRawRootMock(root, rootSet);
    EXPECT_EQ(rootSet.size(), 1);
    EXPECT_EQ(rootSet.back(), obj);

    rootSet.pop_back();
    EXPECT_EQ(rootSet.size(), 0);
}

HWTEST_F_L0(WCollectorTest, PreforwardStaticRootsTest0) {
    std::unique_ptr<WCollector> wcollector = GetWCollector();
    ASSERT_TRUE(wcollector != nullptr);

    std::unique_ptr<WCollectorMock> wcollectorMock = GetWCollectorMock();
    ASSERT_TRUE(wcollectorMock != nullptr);

    RuntimeParam runtimePara = BaseRuntimeParam::DefaultRuntimeParam();
    runtimePara.gcParam.enableStwGC = true;
    wcollectorMock->Init(runtimePara);

    Mutator::GetMutator()->SetMutatorPhase(GCPhase::GC_PHASE_MARK);
    Mutator::GetMutator()->SetSuspensionFlag(Mutator::SuspensionType::SUSPENSION_FOR_GC_PHASE);
    Mutator::GetMutator()->TransitionGCPhase(true);

    ecmascript::DynamicObjectOperator::Initialize();

    RefFieldVisitor visitor = [](RefField<>& refField) {
        RefField<> oldField(refField);
        BaseObject* oldObj = oldField.GetTargetObject();

        oldObj->SetForwardState(BaseStateWord::ForwardState::NORMAL);

        if(reinterpret_cast<MAddress>(oldObj) == 0x7ffb19a09050) {
            std::cout << "oldObj" << std::endl;
        }

        auto taggedObject = ecmascript::TaggedObject::Cast(oldObj);
        ecmascript::JSHClass *jsHclass = taggedObject->GetClass();
        jsHclass->SetObjectType(ecmascript::JSType::FREE_OBJECT_WITH_ONE_FIELD);
        jsHclass->SetObjectSize(1);
        jsHclass->GetClass()->SetObjectType(ecmascript::JSType::HCLASS);
        // jsHclass->GetClass()->SetObjectSize(1);
        jsHclass->GetClass()->SetForwardState(BaseStateWord::ForwardState::NORMAL);

        RegionDesc* region = RegionDesc::GetRegionDescAt(reinterpret_cast<MAddress>(oldObj));
        region->SetRegionType(RegionDesc::RegionType::FROM_REGION);
    };

    WeakRefFieldVisitor weakVisitor = [](RefField<> &refField) -> bool {
        RefField<> oldField(refField);
        BaseObject *oldObj = oldField.GetTargetObject();

        oldObj->SetForwardState(BaseStateWord::ForwardState::NORMAL);
        oldObj->SetSizeForwarded(16);

        if(reinterpret_cast<MAddress>(oldObj) == 0x7ffb19a09050) {
            std::cout << "weak oldObj" << std::endl;
        }

        auto taggedObject = ecmascript::TaggedObject::Cast(oldObj);
        ecmascript::JSHClass *jsHclass = taggedObject->GetClass();
        jsHclass->SetObjectType(ecmascript::JSType::FREE_OBJECT_WITH_ONE_FIELD);
        jsHclass->SetObjectSize(1);
        jsHclass->GetClass()->SetObjectType(ecmascript::JSType::HCLASS);
        jsHclass->GetClass()->SetObjectSize(1);
        jsHclass->GetClass()->SetForwardState(BaseStateWord::ForwardState::NORMAL);
        
        RegionDesc* region = RegionDesc::GetRegionDescAt(reinterpret_cast<MAddress>(oldObj));
        region->SetRegionType(RegionDesc::RegionType::FROM_REGION);
        return true;
    };

    VisitRoots(visitor, false);
    VisitWeakRoots(weakVisitor);

    wcollectorMock->DoGarbageCollectionMock();
}

} // namespace panda::test